﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Weisenz.Core
{
    /// <summary>
    /// HTTP 请求相关处理类。
    /// </summary>
    public class RequestUtil
    {
        /// <summary>
        /// 判断是否是移动设备访问。
        /// </summary>
        public static bool IsMobile
        {
            get
            {
                bool result = false;
                HttpContext context = HttpContext.Current;
                if (context != null)
                {
                    HttpRequest request = context.Request;
                    string useragent = request.UserAgent;
                    if (useragent.IndexOf("android") > -1 || useragent.IndexOf("ipod") > -1 || useragent.IndexOf("iphone") > -1 || useragent.IndexOf("ipad") > -1 || useragent.IndexOf("ucweb") > -1)
                    {
                        result = true;
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// 判断当前页面是否接收了POST请求。
        /// </summary>
        public static bool IsPost
        {
            get { return HttpContext.Current.Request.HttpMethod.Equals("POST"); }
        }

        /// <summary>
        /// 判断当前页面是否接收了GET请求。
        /// </summary>
        public static bool IsGet
        {
            get { return HttpContext.Current.Request.HttpMethod.Equals("GET"); }
        }

        /// <summary>
        /// 取得GET参数值。
        /// </summary>
        /// <param name="key">关键字</param>
        public static string Query(string key)
        {
            string value = HttpContext.Current.Request.QueryString[key];
            return string.IsNullOrEmpty(value) ? string.Empty : value.ToString().Trim();
        }

        /// <summary>
        /// 取得POST参数值。
        /// </summary>
        /// <param name="key">关键字</param>
        public static string GetPostString(string key)
        {
            string value = HttpContext.Current.Request.Form[key];
            return string.IsNullOrEmpty(value) ? string.Empty : value.ToString().Trim();
        }

        /// <summary>
        /// 取得客户端的IP地址。
        /// </summary>
        public static string GetClientIP
        {
            get
            {
                string ip = string.Empty;
                try
                {
                    HttpRequest request = HttpContext.Current.Request;
                    if (request.ServerVariables["HTTP_VIA"] != null) { ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim(); }
                    else { ip = request.UserHostAddress; }
                }
                catch {
                    ip = "无法获取";
                }
                return ip;
            }
        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(string strName, string key)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
                return HttpContext.Current.Request.Cookies[strName][key].ToString();
            else
            {
                return string.Empty;
            }
        }
    }
}
